<template>
  <div class="app-container">
    <div ref="topForm">
      <el-form
        :model="queryParams"
        ref="queryForm"
        size="small"
        :inline="true"
        v-show="showSearch"
        label-width="6.25rem"
      >
        <el-form-item label="客户名" prop="clientName">
          <el-input
            v-model="queryParams.clientName"
            placeholder="请输入客户名"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="计划名称" prop="planName">
          <el-input
            v-model="queryParams.planName"
            placeholder="请输入计划名称"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="计划开始时间" prop="planStartDate">
          <el-date-picker
            clearable
            v-model="queryParams.planStartDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择计划开始时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="计划结束时间" prop="planEndDate">
          <el-date-picker
            clearable
            v-model="queryParams.planEndDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择计划结束时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="维保单位" prop="wbCompanyId">
          <el-select
            v-model="queryParams.wbCompanyId"
            filterable
            clearable
            placeholder="请输入维保单位"
          >
            <el-option
              v-for="(item, index) in maintainCompanyList"
              :key="index"
              :label="item.companyName"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="维保单位" prop="wbCompanyName">
          <el-input
            v-model="queryParams.wbCompanyName"
            placeholder="请输入维保单位"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item> -->
        <el-form-item label="技术负责人" prop="engineerName">
          <el-input
            v-model="queryParams.engineerName"
            placeholder="请输入技术负责人姓名"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>

        <el-form-item label="项目负责人" prop="fireFightingName">
          <el-input
            v-model="queryParams.fireFightingName"
            placeholder="请输入项目负责人姓名"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>

        <el-form-item label="操作者" prop="operaterName">
          <el-input
            v-model="queryParams.operaterName"
            placeholder="请输入操作者姓名"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-search"
            size="mini"
            @click="handleQuery"
            >搜索</el-button
          >
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
            >重置</el-button
          >
        </el-form-item>
      </el-form>
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['wb:wbPlan:add']"
            >新增</el-button
          >
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="danger"
            plain
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['wb:wbPlan:remove']"
            >删除</el-button
          >
        </el-col>
        <!-- <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
            v-hasPermi="['wb:wbPlan:export']"
            >导出</el-button
          >
        </el-col> -->
        <right-toolbar
          :showSearch.sync="showSearch"
          @queryTable="getList"
        ></right-toolbar>
      </el-row>
    </div>

    <el-table v-loading="loading" :data="wbPlanList" :key="isTable">
      <!--      :height="`calc(100vh - ${topHeight} - 200px)`" -->
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <!-- <el-table-column label="主键id" align="center" prop="id" />
      <el-table-column label="客户id" align="center" prop="clientId" /> -->
      <!-- <el-table-column label="序号" align="center" prop="id" width="55" /> -->
      <!-- <el-table-column label="客户名" align="center" prop="clientName" />
      <el-table-column label="客户地址" align="center" prop="clientAddress" /> -->
      <el-table-column
        label="维保单位"
        align="center"
        prop="wbCompanyName"
        width="200"
        show-overflow-tooltip
      />
      <el-table-column label="计划类型" align="center" prop="planType">
        <template slot-scope="scope">
          <span>{{ fomatterLabelName("wbPlanType", scope.row.planType) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="项目负责人"
        align="center"
        prop="fireFightingName"
      />
      <el-table-column label="技术负责人" align="center" prop="engineerName" />
      <el-table-column label="计划名称" align="center" prop="planName" />
      <el-table-column label="操作员" align="center" prop="operaterName" />
      <el-table-column
        label="计划开始时间"
        align="center"
        prop="planStartDate"
        width="120"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.planStartDate, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="计划结束时间"
        align="center"
        prop="planEndDate"
        width="120"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.planEndDate, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="实际开始时间" align="center" prop="execStartTime" width="120">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.execStartTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="实际结束时间" align="center" prop="execEndTime" width="120">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.execEndTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column> -->
      <!-- <el-table-column label="合同id" align="center" prop="contractId" />
      <el-table-column label="维保单位id" align="center" prop="wbCompanyId" /> -->
      <!-- <el-table-column label="任务数" align="center" prop="contentNum" />
      <el-table-column label="待执行数" align="center" prop="executory" /> -->
      <!-- <el-table-column label="项目负责人" align="center" prop="projectPrincipalName" />
      <el-table-column label="技术负责人" align="center" prop="technololyPrincipalName" /> -->
      <!-- <el-table-column label="操作者id" align="center" prop="operaterId" /> -->
      <!-- <el-table-column label="负责人" align="center" prop="projectPrincipalId" /> -->
      <el-table-column label="执行状态" align="center" prop="execStatus">
        <template slot-scope="scope">
          {{ fomatterLabelName("wbStatus", scope.row.execStatus) }}
        </template>
      </el-table-column>
      <!-- <el-table-column label="审核状态" align="center" prop="auditStatus" width="120"/> -->
      <el-table-column
        label="完成时间"
        align="center"
        prop="finishTime"
        width="120"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.finishTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="凭条" align="center" prop="entrySlip" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.entrySlip" :width="50" :height="50" />
        </template>
      </el-table-column>
      <el-table-column label="现场照片" align="center" prop="sitePhoto" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.sitePhoto" :width="50" :height="50" />
        </template>
      </el-table-column> -->
      <!-- <el-table-column label="描述" align="center" prop="remark" /> -->
      <el-table-column label="创建时间" align="center" prop="createTime" />
      <el-table-column label="操作" align="center" width="180" fixed="right">
        <template slot-scope="scope">
          <!-- <el-button size="mini" type="text" icon="el-icon-edit" @click="execute(scope.row)"
            v-hasPermi="['wb:wbPlan:edit']">执行</el-button> -->
          <el-button size="mini" type="text" @click="handleViews(scope.row)"
            >查看</el-button
          >

          <el-button
            v-show="isShow == 'true'"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['wb:wbPlan:edit']"
            :disabled="scope.row.execStatus == 5 || scope.row.execStatus == 6"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            v-show="isShow == 'true'"
            icon="el-icon-delete"
            v-if="scope.row.execStatus == 0"
            @click="handleDelete(scope.row)"
            v-hasPermi="['wb:wbPlan:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改维保计划对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="56.25rem"
      append-to-body
    >
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        :disabled="allDisabled"
        label-width="7.5rem"
        class="dialog-form"
        :inline="true"
      >
        <!-- <el-row>
          <el-col :span="22">
            <el-form-item label="计划名称" prop="planName" class="form-item-row">
              <el-input v-model="form.planName" placeholder="请输入计划名称" />
            </el-form-item>
          </el-col>
        </el-row> -->
        <el-row>
          <el-col :span="12">
            <el-form-item label="计划类型" prop="planType">
              <el-select
                v-model="form.planType"
                filterable
                clearable
                :disabled="!isAdd"
                placeholder="请输入客户"
                @change="planTypeChange"
              >
                <el-option
                  v-for="(item, index) in Dictionary.wbPlanType"
                  :key="index"
                  :label="item.label"
                  :value="item.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="合同" prop="contractId">
              <el-select
                v-model="form.contractId"
                filterable
                clearable
                placeholder="请输入合同"
                @change="contractChange"
              >
                <el-option
                  v-for="(item, index) in contractList"
                  :key="index"
                  :label="item.name"
                  :value="item.id"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="业主单位" prop="clientId">
              <el-select
                v-model="form.clientId"
                filterable
                clearable
                placeholder="请输入客户"
                disabled
              >
                <el-option
                  v-for="(item, index) in componentList"
                  :key="index"
                  :label="item.entName"
                  :value="item.id"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="服务机构" prop="wbCompanyId">
              <el-select
                v-model="form.wbCompanyId"
                filterable
                clearable
                placeholder="请输入维保单位"
                disabled
                @change="companyChange"
              >
                <el-option
                  v-for="(item, index) in maintainCompanyList"
                  :key="index"
                  :label="item.companyName"
                  :value="item.id"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row v-if="form.planType == 3">
          <el-col :span="12">
            <el-form-item label="计划开始时间" prop="planStartDate">
              <el-date-picker
                clearable
                v-model="form.planStartDate"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="请选择计划开始时间"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="计划结束时间" prop="planEndDate">
              <el-date-picker
                clearable
                v-model="form.planEndDate"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="请选择计划结束时间"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row v-if="isAdd && form.planType == 0">
          <el-col :span="24">
            <el-form-item label="计划月份" prop="selectMonths">
              <el-date-picker
                type="months"
                v-model="form.selectMonths"
                placeholder="选择一个或多个月"
                value-format="yyyy-MM"
                @input="selectMonthsChange"
              >
              </el-date-picker>
            </el-form-item>

            <el-timeline style="width: 355px">
              <el-timeline-item
                v-for="(item, index) in form.selectMonths"
                :key="index"
                icon=""
                size="normal"
                type=""
                :timestamp="item"
              >
                {{ "月检计划" + (index + 1) }}
              </el-timeline-item>
            </el-timeline>
          </el-col>
        </el-row>
        <!-- <el-row>
          <el-col :span="12">
            <el-form-item label="客户名" prop="clientName">
              <el-input v-model="form.clientName" placeholder="请输入客户名" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="客户地址" prop="clientAddress">
              <el-input v-model="form.clientAddress" type="textarea" placeholder="请输入内容" />
            </el-form-item>
          </el-col>
        </el-row> -->
        <el-row>
          <el-col :span="12" v-if="!isYear">
            <el-form-item label="类目" prop="classifyList" style="width: 100%">
              <el-select
                class="classify"
                v-model="selectClassList"
                multiple
                placeholder="请选择类目"
                @focus="classSelection"
                ref="classSelect"
                @remove-tag="onRemoveTag"
              >
                <el-option
                  v-for="item in classifyList"
                  :key="item.id"
                  :label="item.classifyName"
                  :value="item.id"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <!-- <el-col :span="12">
            <el-form-item label="负责人" prop="projectPrincipalId">
              <el-input v-model="form.projectPrincipalId" placeholder="请输入负责人" style="width: 13.75rem;" />
            </el-form-item>
          </el-col> -->
          <el-col :span="12">
            <el-form-item label="操作员" prop="operaterId">
              <el-select
                v-model="form.operaterId"
                placeholder="请选择操作者"
                @change="operaterChange"
              >
                <el-option
                  v-for="item in implementerList"
                  :key="item.userId"
                  :label="item.nickName"
                  :value="item.userId"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <!-- <el-col :span="12">
            <el-form-item label="完成时间" prop="finishTime">
              <el-date-picker clearable v-model="form.finishTime" type="date" value-format="yyyy-MM-dd"
                placeholder="请选择完成时间">
              </el-date-picker>
            </el-form-item>
          </el-col> -->
          <!-- <el-col :span="12">
            <el-form-item label="消防师" prop="operaterId">
              <el-select v-model="form.fireFightingId" placeholder="请选择类目" @change="fireChange">
                <el-option v-for="item in firefighterList" :key="item.userId" :label="item.nickName"
                  :value="item.userId">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col> -->
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="项目负责人" prop="fireFightingId">
              <el-select
                v-model="form.fireFightingId"
                placeholder="请选择类目"
                @change="fireChange"
              >
                <el-option
                  v-for="item in firefighterList"
                  :key="item.userId"
                  :label="item.nickName"
                  :value="item.userId"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="技术负责人" prop="engineerId">
              <el-select
                v-model="form.engineerId"
                placeholder="请选择类目"
                @change="engineerChange"
              >
                <el-option
                  v-for="item in engineerList"
                  :key="item.userId"
                  :label="item.nickName"
                  :value="item.userId"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row v-if="isYear">
          <el-col :span="12">
            <el-form-item label="检测编号" prop="detectionNumber">
              <el-input
                v-model="form.detectionNumber"
                placeholder="请输入内容"
              ></el-input>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="设施名称" prop="facilityName">
              <el-input
                v-model="form.facilityName"
                placeholder="请输入内容"
              ></el-input>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="应用场所" prop="applicationPlace">
              <el-input
                v-model="form.applicationPlace"
                placeholder="请输入内容"
              ></el-input>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="检测类别" prop="detectCategory">
              <!-- <el-input
                v-model="form.detectCategory"
                placeholder="请输入内容"
              ></el-input> -->

              <el-select v-model="form.detectCategory" placeholder="检测类别">
                <el-option label="年度检测" value="年度检测"> </el-option>

                <el-option label="竣工检测" value="竣工检测"> </el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="应用场所地址" prop="applicationPlaceAddr">
              <el-input
                v-model="form.applicationPlaceAddr"
                placeholder="请输入内容"
              ></el-input>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="复检时间" prop="reinspectionTime">
              <el-date-picker
                value-format="yyyy-MM-dd"
                v-model="form.reinspectionTime"
                type="date"
                placeholder="选择复检时间"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="联系电话" prop="contactNumber">
              <el-input
                v-model="form.contactNumber"
                placeholder="请输入内容"
              ></el-input>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="备案编号" prop="recordNumber">
              <el-input
                v-model="form.recordNumber"
                placeholder="请输入内容"
              ></el-input>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="备案时间" prop="filingTime">
              <el-date-picker
                value-format="yyyy-MM-dd"
                v-model="form.filingTime"
                type="date"
                placeholder="选择备案时间"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>

        <!-- <el-form-item label="技术负责人" prop="technololyPrincipalId">
          <el-input v-model="form.technololyPrincipalId" placeholder="请输入技术负责人id" style="width: 13.75rem;" />
        </el-form-item> -->
        <!-- <el-form-item label="负责人项目" prop="projectPrincipalName">
          <el-input v-model="form.projectPrincipalName" placeholder="请输入负责人项目" />
        </el-form-item> -->
        <!-- <el-form-item label="技术负责人姓名" prop="technololyPrincipalName">
          <el-input v-model="form.technololyPrincipalName" placeholder="请输入技术负责人姓名" />
        </el-form-item> -->
        <!-- <el-form-item label="操作者姓名" prop="operaterName">
          <el-input v-model="form.operaterName" placeholder="请输入操作者姓名" />
        </el-form-item> -->
        <!-- <el-form-item label="维保单位" prop="wbCompanyName">
          <el-input v-model="form.wbCompanyName" placeholder="请输入维保单位" />
        </el-form-item> -->
        <!-- <el-form-item label="任务数" prop="contentNum">
          <el-input v-model="form.contentNum" placeholder="请输入任务数" />
        </el-form-item>
        <el-form-item label="待执行数" prop="executory">
          <el-input v-model="form.executory" placeholder="请输入待执行数" />
        </el-form-item> -->
        <!-- <el-form-item label="描述" prop="remark" class="form-item-row">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入描述" />
        </el-form-item> -->
        <!-- <el-form-item label="凭条" prop="entrySlip" class="row">
          <image-upload v-model="form.entrySlip" />
        </el-form-item>
        <el-form-item label="现场照片" prop="sitePhoto" class="row">
          <image-upload v-model="form.sitePhoto" />
        </el-form-item> -->
        <!-- <el-form-item label="删除状态(1存在，2删除)" prop="delFlag">
          <el-input v-model="form.delFlag" placeholder="请输入删除状态(1存在，2删除)" />
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" v-if="!loadingBtn"
          >确 定</el-button
        >
        <el-button type="primary" :loading="true" v-else>加载中</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog title="类目列表" :visible.sync="dialogTableVisible">
      <el-table
        ref="classTable"
        row-key="id"
        v-loading="loading"
        :data="classifyList"
        @select="seleChange"
        @select-all="allChange"
        :key="tableKey"
      >
        <el-table-column
          type="selection"
          width="55"
          prop="isCheckedStatus"
          :reserve-selection="true"
        >
          <!-- <template slot-scope="{row}">
            <el-radio v-model="row.isCheckedStatus" label="1" @change="classCheck(row)"></el-radio>
          </template> -->
        </el-table-column>
        <el-table-column label="名称" align="center" prop="classifyName">
          <template slot-scope="scope">
            {{ scope.row.classifyName || scope.row.name }}
          </template>
        </el-table-column>
        <el-table-column label="状态" align="center" prop="status">
          <template slot-scope="{ row }">
            {{ row.status == 0 ? "正常" : "停用" }}
          </template>
        </el-table-column>
        <!-- <el-table-column label="描述" align="center" prop="remark" /> -->
        <!-- <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
              v-hasPermi="['wb:wbClassify:edit']">修改</el-button>
            <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
              v-hasPermi="['wb:wbClassify:remove']">删除</el-button>
          </template>
        </el-table-column> -->
      </el-table>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogTableVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleSelectionChange"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  listWbPlan,
  getWbPlan,
  delWbPlan,
  addWbPlan,
  updateWbPlan,
  newAddWbPlan,
  newUpdateWbPlan,
  newDelWbPlan,
  getWbDetail,
} from "@/api/wb/wbPlan";
import { listWbUserList } from "@/api/wb/userManage.js";
import { listWbClassifys } from "@/api/wb/wbClassify";
import { fomatterLabelName } from "@/utils/index.js";
import { listWbEntAuth } from "@/api/wb/wbEntAuth.js";
import { listWbMaintainCompany } from "@/api/wb/wbMaintainCompany.js";
import { listWbContract } from "@/api/wb/wbContract.js";
import Dictionary from "@/utils/dictionary";
import dayjs from "dayjs";
export default {
  name: "WbPlan",
  data() {
    return {
      Dictionary,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 维保计划表格数据
      wbPlanList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      allDisabled: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        clientId: null,
        clientName: null,
        clientAddress: null,
        planType: null,
        planName: null,
        planStartDate: null,
        planEndDate: null,
        execStartTime: null,
        execEndTime: null,
        contractId: null,
        wbCompanyId: null,
        wbCompanyName: null,
        contentNum: null,
        executory: null,
        projectPrincipalId: null,
        projectPrincipalName: null,
        technololyPrincipalId: null,
        technololyPrincipalName: null,
        operaterId: null,
        operaterName: null,
        execStatus: null,
        auditStatus: null,
        finishTime: null,
        facilityName: null,
        applicationPlace: null,
        detectCategory: null,
        applicationPlaceAddr: null,
        reinspectionTime: null,
        contactNumber: null,
        recordNumber: null,
        filingTime: null,
        detectionNumber: null,
      },
      // 表单参数
      form: {},
      executeForm: {},
      // 表单校验
      rules: {
        planType: [
          {
            required: true,
            message: "计划类型不能为空",
            trigger: "blur",
          },
        ],
        contractId: [
          {
            required: true,
            message: "合同不能为空",
            trigger: "blur",
          },
        ],

        clientId: [
          {
            required: true,
            message: "业主单位不能为空",
            trigger: "blur",
          },
        ],

        wbCompanyId: [
          {
            required: true,
            message: "服务机构不能为空",
            trigger: "blur",
          },
        ],

        classifyList: [
          {
            required: true,
            message: "类目不能为空",
            trigger: "blur",
          },
        ],
        fireFightingId: [
          {
            required: true,
            message: "请选择项目负责人",
            trigger: "blur",
          },
        ],
        operaterId: [
          {
            required: true,
            message: "请选择操作者",
            trigger: "blur",
          },
        ],

        engineerId: [
          {
            required: true,
            message: "请选择操作者",
            trigger: "blur",
          },
        ],
      },
      componentList: [],
      maintainCompanyList: [],
      contractList: [],
      topHeight: null,
      isTable: true,
      classifyList: [],
      firefighterList: [],
      engineerList: [],
      implementerList: [],
      dialogTableVisible: false,
      classifyIdList: [],
      selectClassList: [],
      isYear: false,
      loadingBtn: false,
      tableKey: true,
      selectMonths: null,
      isAdd: false,
      gcsList: [],
      isShow: false,
    };
  },
  created() {
    this.getList();
    // 查询所有业主公司
    this.getCompanyList();
    // 查询所有维保公司
    this.getWbList();
    // 查询所有合同
    this.getContractList();
    this.getWbClassify();
    this.isShow = window.localStorage.getItem("isShow");
  },
  mounted() {
    this.$nextTick(() => {
      this.topHeight = this.$refs.topForm.offsetHeight + "px";
    });
    // this.getEngineer()
    // this.getFirefighter()
    // this.listWbUserList()

    this.getUserInfo();
  },
  watch: {
    showSearch() {
      this.$nextTick(() => {
        this.topHeight = this.$refs.topForm.offsetHeight + "px";
        this.isTable = !this.isTable;
      });
    },
  },
  methods: {
    fomatterLabelName,
    getCompanyList() {
      listWbEntAuth({ state: 1 }).then((data) => {
        this.componentList = data.rows;
      });
    },
    getWbList() {
      listWbMaintainCompany().then((data) => {
        this.maintainCompanyList = data.rows;
      });
    },
    getContractList() {
      listWbContract().then((data) => {
        this.contractList = data.rows;
      });
    },
    /** 查询维保计划列表 */
    getList() {
      this.loading = true;
      listWbPlan(this.queryParams).then((response) => {
        this.wbPlanList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    async getUserInfo() {
      // this.firefighterList = await this.getUsers('一级消防师')
      // this.engineerList = await this.getUsers('工程师')
      // this.firefighterList = await this.getUsers('工程师')
      this.gcsList = await this.getUsers("工程师");
      this.engineerList = this.gcsList;
      this.firefighterList = this.gcsList;
      this.implementerList = await this.getUsers("操作员");
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        clientId: null,
        clientName: null,
        clientAddress: null,
        planType: null,
        planName: null,
        planStartDate: null,
        planEndDate: null,
        execStartTime: null,
        execEndTime: null,
        contractId: null,
        wbCompanyId: null,
        wbCompanyName: null,
        contentNum: null,
        executory: null,
        projectPrincipalId: null,
        projectPrincipalName: null,
        technololyPrincipalId: null,
        technololyPrincipalName: null,
        operaterId: null,
        operaterName: null,
        execStatus: null,
        auditStatus: null,
        finishTime: null,
        entrySlip: null,
        sitePhoto: null,
        delFlag: null,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null,
        remark: null,
        classifyList: null,
        engineerId: null,
        engineerName: null,
        fireFightingId: null,
        fireFightingName: null,
        selectMonths: null,
        facilityName: null,
        applicationPlace: null,
        detectCategory: null,
        applicationPlaceAddr: null,
        reinspectionTime: null,
        contactNumber: null,
        recordNumber: null,
        filingTime: null,
        detectionNumber: null,
      };
      this.selectClassList = [];
      this.resetForm("form");
    },
    executeReset() {
      this.form = {
        id: null,
        remark: null,
      };
      this.resetForm("execute-form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.allDisabled = false;
      this.open = true;
      this.title = "添加维保计划";
      this.getWbClassify();
      this.tableKey = !this.tableKey;
      this.isAdd = true;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.allDisabled = false;

      const id = row.id || this.ids;
      this.isAdd = false;
      getWbDetail(id).then((response) => {
        this.form = response.data;
        this.form.selectMonths = [];
        this.form.planType = Number(response.data.planType);
        // this.classifyList = []
        if (response.data.planClassifyVos.length) {
          response.data.planClassifyVos.forEach((item) => {
            item.id = item.classifyId;
            // this.classifyList.push(item)
            if (item.isCheckedStatus == true) {
              this.selectClassList.push(item.id + "");
            }
          });

          this.classifyList.forEach((item, index) => {
            item.isCheckedStatus =
              response.data.planClassifyVos[index].isCheckedStatus;
          });
          this.selectClassList = [];
        }

        this.$set(this.form, "classifyList", this.classifyList);
        if (response.data.planType == "0") {
          this.isYear = false;
        } else if (response.data.planType == "3") {
          this.isYear = true;
        }
        // this.form.classifyList = response.data.planClassifyVos
        this.tableKey = !this.tableKey;
        this.open = true;
        this.handleSelectionChange();
        this.title = "修改维保计划";
      });
    },

    handleViews(row) {
      this.reset();
      this.allDisabled = true;
      const id = row.id || this.ids;
      this.isAdd = false;
      getWbDetail(id).then((response) => {
        this.form = response.data;
        this.form.selectMonths = [];
        this.form.planType = Number(response.data.planType);
        // this.classifyList = []
        if (response.data.planClassifyVos.length) {
          response.data.planClassifyVos.forEach((item) => {
            item.id = item.classifyId;
            // this.classifyList.push(item)
            if (item.isCheckedStatus == true) {
              this.selectClassList.push(item.id + "");
            }
          });

          this.classifyList.forEach((item, index) => {
            item.isCheckedStatus =
              response.data.planClassifyVos[index].isCheckedStatus;
          });
          this.selectClassList = [];
        }

        this.$set(this.form, "classifyList", this.classifyList);
        if (response.data.planType == "0") {
          this.isYear = false;
        } else if (response.data.planType == "3") {
          this.isYear = true;
        }
        // this.form.classifyList = response.data.planClassifyVos
        this.tableKey = !this.tableKey;
        this.open = true;
        this.handleSelectionChange();
        this.title = "查看维保计划";
      });
    },
    // 合同改变
    contractChange(v) {
      let data = this.contractList.find((item) => v == item.id);
      if (data) {
        console.log(data, "data");
        this.form.clientId = data.clientId;
        this.form.clientName = data.clientName;

        this.form.wbCompanyId = data.wbCompanyId;
        this.form.wbCompanyName = data.wbCompayName;
        console.log(this.form, "fom");
      }
    },
    /** 提交按钮 */
    submitForm() {
      if (this.form.planType == 3) {
        if (
          new Date(this.form.planStartDate) > new Date(this.form.planEndDate)
        ) {
          this.$modal.msgWarning("请选择正确的计划时间！");

          return;
        }
      }

      // this.form.selectMonths = [{ startDate: this.form.planStartDate, endDate: this.form.endDate }]
      this.$refs["form"].validate((valid) => {
        let data = this.componentList.findLast(
          (item) => item.id == this.form.clientId
        );

        // if (this.form.planType == 0) {
        //   if (!data || this.form.planStartDate == "") return;
        //   let year = new Date(this.form.planStartDate).getFullYear();
        //   let moth = (
        //     "0" +
        //     (new Date(this.form.planStartDate).getMonth() + 1)
        //   ).slice(-2);
        //   let day = ("0" + new Date(this.form.planStartDate).getDate()).slice(
        //     -2
        //   );
        //   this.form.planName = `${data.entName}${year}年${moth}月${day}日月度维保计划`;
        // } else {
        //   console.log(data, "9999");
        //   console.log(this.form.planStartDate, "3333333");

        //   if (!data || this.form.planStartDate == "") return;

        //   let year = new Date(this.form.planStartDate).getFullYear();
        //   this.form.planName = `${data.entName}${year}年度维保计划`;
        // }

        if (valid) {
          this.loadingBtn = true;
          if (this.form.id != null) {
            this.form.planId = this.form.id;

            if (this.form.planType == 3) {
              this.form.classifyList = [];
            }
            newUpdateWbPlan(this.form)
              .then((response) => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
                this.loadingBtn = false;
              })
              .catch(() => {
                this.loadingBtn = false;
              });
          } else {
            newAddWbPlan(this.form)
              .then((response) => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
                this.loadingBtn = false;
              })
              .catch(() => {
                this.loadingBtn = false;
              });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm("是否确认删除维保计划？").then(() => {
        newDelWbPlan([ids]).then((res) => {
          if (res.code == 200) {
            this.getList();
            this.$modal.msgSuccess("删除成功");
          }
        });
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "wb/wbPlan/export",
        {
          ...this.queryParams,
        },
        `wbPlan_${new Date().getTime()}.xlsx`
      );
    },
    // 执行
    execute(row) {
      this.title = "执行维保计划";
    },
    // 获取类目列表
    getWbClassify() {
      listWbClassifys().then((res) => {
        if (res.code == 200) {
          this.classifyList = [...res.rows];
          this.classifyList.map((item) => {
            item.isCheckedStatus = false;
          });
        }
      });
    },
    // 选多个月
    selectMonthsChange(v) {},
    // 修改选中类目
    onRemoveTag(value) {
      console.log(value, 888);
    },
    // 获取消防师列表
    getFirefighter() {
      listWbUserList({ maintainCompanyRole: 2 }).then((res) => {
        if (res.code !== 200) return;
        this.firefighterList = [...res.rows];
      });
    },
    fireChange(value) {
      let firefighter = this.firefighterList.find(
        (item) => item.userId == value
      );
      if (!firefighter) return;
      this.engineerList = this.gcsList.filter((item) => item.id != value);
      this.$set(this.form, "fireFightingName", firefighter.nickName);
    },
    planTypeChange(value) {
      if (value == "3") {
        this.isYear = true;
      } else {
        this.isYear = false;
        this.form.facilityName = "";
        this.form.applicationPlace = "";
        this.form.detectCategory = "";
        this.form.applicationPlaceAddr = "";
        this.form.reinspectionTime = "";
        this.form.contactNumber = "";
        this.form.recordNumber = "";
        this.form.detectionNumber = "";
        this.form.filingTime = "";
      }
    },
    // 获取工程师列表
    getEngineer() {
      listWbUserList({ maintainCompanyRole: 1 }).then((res) => {
        if (res.code !== 200) return;
        this.engineerList = [...res.rows];
      });
    },
    engineerChange(value) {
      let engineer = this.engineerList.find((item) => item.userId == value);
      if (!engineer) return;
      this.firefighterList = this.gcsList.filter((item) => item.id != value);
      this.$set(this.form, "engineerName", engineer.nickName);
    },
    // 获取实施者列表
    listWbUserList() {
      listWbUserList({ maintainCompanyRole: 3 }).then((res) => {
        if (res.code !== 200) return;
        this.implementerList = [...res.rows];
      });
    },
    operaterChange(value) {
      let operater = this.implementerList.find((item) => item.userId == value);
      if (!operater) return;
      this.$set(this.form, "operaterName", operater.nickName);
    },

    seleChange(seletion, row) {
      this.classifyList.forEach((item) => {
        if (item.id == row.id) {
          item.isCheckedStatus = !item.isCheckedStatus;
        }
      });
      // this.selectClassList = []
      // seletion.forEach(item => {
      //   this.selectClassList.push(item.id)
      // })
      // this.$set(this.form, 'classifyList', [...this.classifyList])
    },
    allChange() {
      this.classifyList.forEach((item) => {
        item.isCheckedStatus = !item.isCheckedStatus;
      });
    },
    classSelection() {
      this.dialogTableVisible = true;
      this.$refs.classSelect.blur();
      this.$nextTick(() => {
        this.$refs.classTable.clearSelection();
        this.classifyList.forEach((item) => {
          if (item.isCheckedStatus) {
            this.$refs.classTable.toggleRowSelection(item, true);
          }
        });
      });
    },

    classCheck(row) {
      this.classifyList.map((item) => {
        if (row.id == item.id) {
          item.isCheckedStatus = !item.isCheckedStatus;
        }
      });
    },

    handleSelectionChange(selection) {
      this.selectClassList = [];
      this.classifyList.forEach((item) => {
        if (item.isCheckedStatus) {
          this.selectClassList.push(item.id);
        }
      });
      this.$set(this.form, "classifyList", [...this.classifyList]);
      this.dialogTableVisible = false;
    },
    companyChange(e) {
      let company = this.maintainCompanyList.find((item) => item.id == e);
      this.form.wbCompanyName = company.companyName;
    },
  },
};
</script>
<style scoped>
.el-tag__close.el-icon-close {
  display: none !important;
}

::v-deep.classify .el-tag.el-tag--info .el-tag__close {
  display: none !important;
}

.date_form {
  .el-year-table td.current:not(.disabled) .cell {
    font-weight: bold !important;
    background-color: var(--el-datepicker-active-color);
    border-radius: 100px;
  }
  .el-year-table td.today .cell {
    font-weight: normal;
    color: #606266;
  }
  .el-month-table td.current:not(.disabled) .cell {
    font-weight: bold !important;
    background-color: var(--el-datepicker-active-color);
    border-radius: 100px;
  }
  .el-month-table td.today .cell {
    font-weight: normal;
    color: #606266;
  }
  .el-date-table td.today .el-date-table-cell__text {
    color: #606266;
    font-weight: normal;
  }
  .el-date-table td.current:not(.disabled) .el-date-table-cell__text {
    font-weight: bold !important;
    background-color: var(--el-datepicker-active-color);
  }
}
</style>
